-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
move late imports to top #26750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
move late imports to top #26750
Conversation
import numpy as np | ||
|
||
from pandas.core.dtypes.generic import ABCSeries | ||
from pandas.core.dtypes.missing import remove_na_arraylike | ||
from pandas.core.reshape.concat import concat |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
import from pandas
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks! Just updated it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@jreback I still pretty new here. Can I ask why this place could directly use import from pandas not import as previously ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
previously before @datapythonista reorg (just merged)
the plotting code was basically imported during pandas init
and the pandas namespace was not fully defined yet
it’s now essentially decoupled from that so that it a lazy import
this we can use top of module imports rather than import into each function
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Appreciated for this detail explanation. This is helpful.
import numpy as np | ||
|
||
from pandas.core.dtypes.generic import ABCSeries | ||
from pandas.core.dtypes.missing import remove_na_arraylike | ||
|
||
from pandas import concat | ||
from pandas.core.series import Series |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Series as well
@xcz011 probably worth just simply Did you check if in the other files inside Thanks! |
@datapythonista I found some lazy matplotlib imports in other files , such as |
Codecov Report
@@ Coverage Diff @@
## master #26750 +/- ##
==========================================
+ Coverage 91.7% 91.72% +0.02%
==========================================
Files 179 178 -1
Lines 50767 50774 +7
==========================================
+ Hits 46555 46574 +19
+ Misses 4212 4200 -12
Continue to review full report at Codecov.
|
Codecov Report
@@ Coverage Diff @@
## master #26750 +/- ##
==========================================
- Coverage 91.7% 91.69% -0.01%
==========================================
Files 179 179
Lines 50767 50764 -3
==========================================
- Hits 46555 46550 -5
- Misses 4212 4214 +2
Continue to review full report at Codecov.
|
@xcz011 all those were lazy to avoid requiring matplotlib if plotting wasn't used. Now that is managed in So, you can move the other imports to the top too, that's not required and should improve readability of the file. |
@datapythonista Thanks for this detail explanation! I just made change to most of files in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good, couple of comments. You can also move the ones in _core.py
(the pandas and matplotlib ones, you can leave the rest where they are I think).
pandas/plotting/_matplotlib/hist.py
Outdated
@@ -2,6 +2,7 @@ | |||
|
|||
import matplotlib.pyplot as plt | |||
import numpy as np | |||
from scipy.stats import gaussian_kde |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The scipy
ones should be moved. It's the same case as with matplotlib, we don't require it unless you call a function that uses it. In this case, if you call a plot (like the line plot), you'll need matplotlib, but not scipy, which you only need if you call the kde plot.
This is why the tests are failing (not sure if you've seen the CI is in red). Can you restore the scipy ones please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got it! Thanks!
pandas/plotting/_matplotlib/misc.py
Outdated
@@ -1,6 +1,12 @@ | |||
# being a bit too dynamic | |||
from math import pi, sqrt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can delete the comment, I guess it refers to how we were importing before. But doesn't seem to add much value in any case.
@jreback do you want to avoid loading math
unless the user calls the andrews_curves plot, like it was? Or you're ok with this? same for random below
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change these to be np.pi and np.sqrt; we don't use math generally
don't have to load scipy unless user call kdeplot
pandas/plotting/_matplotlib/misc.py
Outdated
@@ -1,6 +1,12 @@ | |||
# being a bit too dynamic | |||
from math import pi, sqrt |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
change these to be np.pi and np.sqrt; we don't use math generally
only import scipy when it is kde and density
looks fine, these are isorted by default right? @WillAyd |
isort doesn't make mods by default in our CI but would fail it if out of order, so this looks good |
Thanks @xcz011 ! |
git diff upstream/master -u -- "*.py" | flake8 --diff